package com.sunny.blockqueue;

import java.util.Queue;
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;

public class Producer implements Runnable{

    private String name;
    private Random random = new Random(System.currentTimeMillis());
    private LinkedBlockingQueue<Integer> queue;

    public Producer(String name, LinkedBlockingQueue<Integer> queue) {
        this.name = name;
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            int product = random.nextInt(10);
            System.out.println(name + "生产" + product + "。当前队列长度为：" + queue.size());
            try {
                queue.put(product);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
